package mall.controller.mall;

import com.google.gson.Gson;
import mall.model.Result;
import mall.model.bo.MallAskGoodsMsgBO;
import mall.model.vo.*;
import mall.service.MallIndexService;
import mall.service.MallIndexServiceImpl;
import mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-06-01 10:34
 *
 * */
@WebServlet("/api/mall/goods/*")
public class GoodsServlet extends HttpServlet {
    private Gson gson = new Gson();
    private MallIndexService mallIndexService = new MallIndexServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/", "");
        if ("askGoodsMsg".equals(action)) {
            askGoodsMsg(request, response);
        }

    }

    /**
     * 商品详情页发送问答
     * 1. 获取请求参数{token: "Yonni", msg: "赛高", goodsId: "1"}
     * 2. 处理业务逻辑
     * 3. 作出响应{"code":0}
     * */
    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        MallAskGoodsMsgBO goodsMsgBO = gson.fromJson(requestBody, MallAskGoodsMsgBO.class);
        mallIndexService.askGoodsMsg(goodsMsgBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/", "");
        if ("getGoodsByType".equals(action)) {
            getGoodsByType(request, response);
        } else if ("searchGoods".equals(action)) {
            searchGoods(request, response);
        } else if ("getGoodsInfo".equals(action)) {
            getGoodsInfo(request, response);
        } else if ("getGoodsMsg".equals(action)) {
            getGoodsMsg(request, response);
        } else if ("getGoodsComment".equals(action)) {
            getGoodsComment(request, response);
        }
    }

    /**
     * 获取商品评价
     * 1. 获取请求参数id(goodsId)
     * */
    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsId = request.getParameter("goodsId");
        MallShowCommentsVO commentsVOS = mallIndexService.getgoodsComment(goodsId);
        response.getWriter().println(gson.toJson(Result.ok(commentsVOS)));
    }

    /**
     * 获取留言信息
     * */
    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<MallGetGoodsMsgVO> goodsMsgVOS = mallIndexService.getGoodsMsg(id);
        response.getWriter().println(gson.toJson(Result.ok(goodsMsgVOS)));
    }

    /**
     * 获取商品信息
     * */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        MallGoodsInfoVO goodsInfoVO = mallIndexService.getGoodsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(goodsInfoVO)));
    }

    /**
     * 商城首页根据关键字搜索商品信息
     * */
    private void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keyword = request.getParameter("keyword");
        List<MallIndexSearchVO> searchVOS = mallIndexService.searchGoods(keyword);
        response.getWriter().println(gson.toJson(Result.ok(searchVOS)));
    }

    /**
     * 商城首页显示商品信息
     * 1. 获取请求参数 typeId：-1表示获取所有商品，1，2，3...表示获取相应type的商品
     * 2. 处理业务逻辑，请求数据库获取数据
     * 3. 作出响应 {"code":0,"data":[{},{}...]}
     * */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        //校验参数
        try {
            Integer.parseInt(typeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<MallShowGoodsVO> goodsVOS = mallIndexService.getGoodsByType(typeId);
        response.getWriter().println(gson.toJson(Result.ok(goodsVOS)));
    }
}
